package com.uchonor.api.config;

import com.uchonor.olight.agent.module.web.vo.ServiceResult;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.context.RequestEntity;
import org.ssssssss.magicapi.core.interceptor.ResultProvider;

@Component
@Primary
public class CustomResultProvider implements ResultProvider {

    private String convertCode(int code) {
        switch (code) {
            case 1:
                return "0000";
            default:
                return "3000";
        }
    }

    @Override
    public Object buildResult(RequestEntity requestEntity, int code, String message, Object data) {
        ServiceResult serviceResult = new ServiceResult();
        serviceResult.setSTATUS(convertCode(code));
        serviceResult.setMSG(message);
        serviceResult.setRSP(convertCode(code), message, data);
        return serviceResult;
    }

    @Override
    public Object buildException(RequestEntity requestEntity, Throwable throwable) {
        return buildResult(requestEntity, 3000, "系统内部出现错误");
    }
}